﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace YasharEl.Infrastructure.ExceptionHandling
{
    public class DefaultExceptionManager : BaseExceptionManager
    {
        #region ReadOnly Fields

        private readonly IDictionary<string, IExceptionPolicy> exceptionPolicies;

        #endregion

        #region Constructors

        public DefaultExceptionManager(IEnumerable<IExceptionPolicy> exceptionPolicies)
            : this(exceptionPolicies.ToDictionary(e => e.PolicyName))
        {
        }

        public DefaultExceptionManager(IDictionary<string, IExceptionPolicy> exceptionPolicies)
        {
            Contract.Requires<ArgumentNullException>(exceptionPolicies != null, "exceptionPolicies could not be null.");
            this.exceptionPolicies = exceptionPolicies;
        }

        #endregion

        #region BaseExceptionManager Overrides

        protected override bool GetExceptionPolicy(string policyName, out IExceptionPolicy exceptionPolicy)
        {
            exceptionPolicy = null;
            if (!exceptionPolicies.TryGetValue(policyName, out exceptionPolicy))
            {
                return false;
            }
            return true;
        }

        #endregion
    }
}
